package com.tmxk.api.days01.cyq.set;

import com.tmxk.api.days01.cyq.Dog;

import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class SetDemo {
    public static void main(String[] args) {
        Dog dog1 = new Dog("小花",2);
        Dog dog2 = new Dog("花花",3);
        Dog dog3 = new Dog("小黑",1);
        Dog dog4 = new Dog("小白",5);
        Dog dog5 = new Dog("汪汪",6);
        //创建set集合对象, 父类对象引用指向子类实例(多态体现)
        HashSet<Dog> dogSet = new HashSet<>();
        dogSet.add(dog1);
        dogSet.add(dog2);
        dogSet.add(dog3);
        dogSet.add(dog4);
        dogSet.add(dog5);
        System.out.println("dogSet的长度："+dogSet.size());
        //获取set集合元素
        System.out.println("使用for循坏遍历 方式一");
        for (Dog dog : dogSet) {
            System.out.println(dog.getName()+ "\t" +dog.getAge() +"\t\t");

        }
        System.out.println("使用lambda表达式遍历  方式二");
        dogSet. forEach(dog ->{

            System.out.println(dog.getName() +"\t" +dog.getAge() + "\t\t");

        });
        System.out.println("\n\n使用迭代器集合元素遍历  方式三");
        // Iterator 可理解成专门为 集合框架遍历元素而产生的
        Iterator<Dog> it = dogSet.iterator();
        // it.hasNext(): 获取下一个迭代元素是否存在
        while(it.hasNext()){
            //  it.next() : 遍历下一个集合元素
            Dog dog =  it.next();
            System.out.println(dog.getName() + "\t" + dog.getAge());
        }

        // TreeSet 存储的数据可排序, 该集合框架底层实现 Comparable 接口, 进行数据比较
        TreeSet dogTreeSet = new TreeSet();
        dogTreeSet.add(dog1);
        dogTreeSet.add(dog2);
        dogTreeSet.add(dog3);
        System.out.println("dogTreeSet的集合长度是:" + dogTreeSet.size());
        System.out.println("循环遍历dogTreeSet 集合 ");
        for (Object obj : dogTreeSet ){
            System.out.println(obj);
        }

    }



}
